<!--待对账客户-->
<template>
  <div class="customers-to-be-reconciled-container">
    <!-- 筛选区域 -->
    <div class="controls-section">
      <button class="btn-new" @click="showCreateDialog">新建客户</button>
      <div class="filters">
        <div class="filter-item">
          <label>客户名称</label>
          <input type="text" v-model="query.customerName" placeholder="请输入客户名称">
        </div>
        <div class="filter-item">
          <label>状态</label>
          <select v-model="query.status">
            <option value="">全部</option>
            <option :value="0">启用</option>
            <option :value="1">禁用</option>
          </select>
        </div>
      </div>
      <div class="actions">
        <button class="btn-primary" @click="search">搜索</button>
        <button class="btn-secondary" @click="reset">重置</button>
      </div>
    </div>

    <!-- 表格区域 -->
    <div class="table-section">
      <table>
                 <thead>
           <tr>
             <th><input type="checkbox" v-model="allSelected"></th>
             <th>客户名称</th>
             <th>客户编号</th>
             <th>联系人</th>
             <th>联系人职位</th>
             <th>联系电话</th>
             <th>微信</th>
             <th>邮箱</th>
             <th>详细地址</th>
             <th>开户行</th>
             <th>银行账号</th>
             <th>状态</th>
           </tr>
         </thead>
         <tbody>
           <tr v-for="(item, index) in customers" :key="item.id">
             <td><input type="checkbox" v-model="item.selected"></td>
             <td class="link-style" :class="{ 'disabled-text': item.status === 1 }" @click="viewDetail(item)">{{ item.customerName }}</td>
             <td>{{ item.customerCode }}</td>
                           <td>{{ item.contactPersonName || '-' }}</td>
             <td>{{ item.contactPosition }}</td>
             <td>{{ item.contactPhone }}</td>
             <td>{{ item.wechat }}</td>
             <td>{{ item.email }}</td>
             <td>{{ item.address }}</td>
             <td>{{ item.bank }}</td>
             <td>{{ item.bankAccountNumber }}</td>
             <td>
               <span
                 class="status-toggle"
                 :class="{ 'status-enabled': item.status === 0, 'status-disabled': item.status === 1 }"
                 @click="toggleStatus(item)"
               >
                 {{ item.status === 0 ? '启用' : '禁用' }}
               </span>
             </td>
           </tr>
         </tbody>
      </table>
    </div>

    <!-- 底部区域 -->
    <div class="footer-section">
      <div class="footer-left">
        <input type="checkbox" v-model="allSelected">
        <label>全选</label>
        <button class="btn-secondary">导出</button>
      </div>
      <div class="pagination">
        <button @click="prevPage" :disabled="query.pageNum <= 1">&lt;</button>
        <button
          v-for="page in pageNumbers"
          :key="page"
          :class="{ active: page === query.pageNum }"
          @click="goToPage(page)"
        >
          {{ page }}
        </button>
        <button @click="nextPage" :disabled="query.pageNum >= totalPages">&gt;</button>
        <select v-model="query.pageSize" @change="onPageSizeChange">
          <option value="5">5条/页</option>
          <option value="10">10条/页</option>
          <option value="20">20条/页</option>
        </select>
      </div>
    </div>

    <!-- 新增客户弹窗 -->
    <div v-if="showCreateModal" class="modal-overlay" @click="closeCreateModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>新增客户</h3>
          <button class="close-btn" @click="closeCreateModal">&times;</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="submitCreate">
                         <div class="form-row">
               <div class="form-group">
                 <label>客户名称 *</label>
                 <input type="text" v-model="createForm.customerName" required>
               </div>

               <div class="form-group">
                 <label>联系人</label>
                 <select v-model="createForm.uId">
                   <option value="">请选择联系人</option>
                   <option v-for="user in userList" :key="user.userId" :value="user.userId">
                     {{ user.realName }} ({{ user.username }})
                   </option>
                 </select>
               </div>
             </div>
                           <div class="form-row">
                <div class="form-group">
                  <label>联系人职位</label>
                  <input type="text" v-model="createForm.contactPosition">
                </div>
                <div class="form-group">
                  <label>联系电话</label>
                  <input type="text" v-model="createForm.contactPhone">
                </div>
              </div>
                             <div class="form-row">
                 <div class="form-group">
                   <label>微信</label>
                   <input type="text" v-model="createForm.wechat">
                 </div>
                 <div class="form-group">
                   <label>邮箱</label>
                   <input type="email" v-model="createForm.email">
                 </div>
               </div>
                              <div class="form-row">
                 <div class="form-group">
                   <label>详细地址</label>
                   <input type="text" v-model="createForm.address">
                 </div>
                 <div class="form-group">
                   <label>开户行</label>
                   <input type="text" v-model="createForm.bank">
                 </div>
               </div>
                              <div class="form-row">
                 <div class="form-group">
                   <label>银行账号</label>
                   <input type="text" v-model="createForm.bankAccountNumber">
                 </div>
                 <div class="form-group">
                   <label>备注</label>
                   <textarea v-model="createForm.remark" rows="3"></textarea>
                 </div>
               </div>
            <div class="form-actions">
              <button type="button" class="btn-secondary" @click="closeCreateModal">取消</button>
              <button type="submit" class="btn-primary">保存</button>
            </div>
          </form>

        </div>
      </div>
    </div>

    <!-- 查看详情抽屉（右侧滑出） -->
    <div v-if="showDetailModal" class="drawer-overlay" @click="closeDetailModal">
      <div class="drawer-content" @click.stop>
        <div class="detail-header">
          <h2>{{ detailData.customerCode }}-客户详情</h2>
          <div class="detail-actions">
            <button class="action-btn">
              <i class="print-icon">🖨️</i>
              打印
            </button>
            <button class="action-btn">
              <i class="share-icon">📤</i>
            </button>
            <button class="action-btn">
              <i class="more-icon">⋯</i>
            </button>
            <button class="close-btn" @click="closeDetailModal">×</button>
          </div>
        </div>

        <div class="detail-body">

          <!-- 客户基础档案 -->
          <div class="detail-section">
            <h3>客户基础档案</h3>
            <div class="detail-form">
              <div class="form-field">
                <label>客户编号</label>
                <input type="text" :value="detailData.customerCode" readonly>
              </div>
              <div class="form-field">
                <label>客户名称</label>
                <input type="text" :value="detailData.customerName" readonly>
              </div>
              <div class="form-field">
                <label class="required">启用状态</label>
                <input type="text" :value="detailData.status === 0 ? '启用' : '禁用'" readonly>
              </div>
              <div class="form-field">
                <label>备注</label>
                <input type="text" :value="detailData.remark || '-'" readonly>
              </div>
            </div>
          </div>

          <!-- 对接人信息 -->
          <div class="detail-section">
            <h3>对接人信息</h3>
            <div class="detail-form">
                             <div class="form-field">
                 <label>对接人</label>
                 <input type="text" :value="detailData.contactPersonName || '-'" readonly>
               </div>
              <div class="form-field">
                <label>职位</label>
                <input type="text" :value="detailData.contactPosition" readonly>
              </div>
              <div class="form-field">
                <label>手机号</label>
                <input type="text" :value="detailData.contactPhone" readonly>
              </div>
              <div class="form-field">
                <label>微信号</label>
                <input type="text" :value="detailData.wechat" readonly>
              </div>
              <div class="form-field">
                <label>邮箱</label>
                <input type="text" :value="detailData.email" readonly>
              </div>
              <div class="form-field">
                <label>地址</label>
                <input type="text" :value="detailData.address" readonly>
              </div>
            </div>
          </div>

          <!-- 财务信息 -->
          <div class="detail-section">
            <h3>财务信息</h3>
            <div class="detail-form">
              <div class="form-field">
                <label>开户行</label>
                <input type="text" :value="detailData.bank" readonly>
              </div>
              <div class="form-field">
                <label>银行账号</label>
                <input type="text" :value="detailData.bankAccountNumber" readonly>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { customerInfoPage, customerInfoAdd, customerInfoDetail, customerInfoUpdateStatus, getUserList } from '@/api/finance/customerInfo'
import { ElMessage } from 'element-plus'
export default {
  name: 'CustomersToBeReconciled',
  data() {
    return {
      customers: [],
      query: {
        pageNum: 1,
        pageSize: 5,
        customerName: '',
        status: null
      },
      total: 0,
      totalPages: 0,
      // 新增客户相关
      showCreateModal: false,
      createForm: {
        customerName: '',
        uId:'',
        contactPosition: '',
        contactPhone: '',
        wechat: '',
        email: '',
        address: '',
        bank: '',
        bankAccountNumber: '',
        remark: ''
      },
             // 查看详情相关
       showDetailModal: false,
       detailData: {},
       // 用户列表
       userList: []
    };
  },
  computed: {
    allSelected: {
      get() {
        return this.customers.length > 0 && this.customers.every(item => item.selected);
      },
      set(value) {
        this.customers.forEach(item => {
          item.selected = value;
        });
      }
    },
    pageNumbers() {
      const pages = [];
      const start = Math.max(1, this.query.pageNum - 2);
      const end = Math.min(this.totalPages, this.query.pageNum + 2);

      for (let i = start; i <= end; i++) {
        pages.push(i);
      }
      return pages;
    }
  },
     mounted() {
     this.load();
     this.loadUserList();
   },
  methods: {
    async load() {
      try {
        const { data } = await customerInfoPage(this.query);
        const pageData = data?.data;
        if (pageData) {
          this.customers = (pageData.records || []).map(x => ({
            ...x,
            selected: false
          }));
          this.total = pageData.total || 0;
          this.totalPages = pageData.pages || 0;
        }
      } catch (error) {
        console.error('加载数据失败:', error);
      }
    },
    search() {
      this.query.pageNum = 1;
      this.load();
    },
    reset() {
      this.query = {
        pageNum: 1,
        pageSize: 10,
        customerName: '',
        status: null
      };
      this.load();
    },
    prevPage() {
      if (this.query.pageNum > 1) {
        this.query.pageNum--;
        this.load();
      }
    },
    nextPage() {
      if (this.query.pageNum < this.totalPages) {
        this.query.pageNum++;
        this.load();
      }
    },
    goToPage(page) {
      this.query.pageNum = page;
      this.load();
    },
    onPageSizeChange() {
      this.query.pageNum = 1;
      this.load();
    },
    showCreateDialog() {
      this.showCreateModal = true;
      this.resetCreateForm();
    },
    closeCreateModal() {
      this.showCreateModal = false;
      this.resetCreateForm();
    },
         resetCreateForm() {
       this.createForm = {
         customerName: '',
         uId: '',
         contactPosition: '',
         contactPhone: '',
         wechat: '',
         email: '',
         address: '',
         bank: '',
         bankAccountNumber: '',
         remark: ''
       };
     },
    async submitCreate() {
      try {
        const response = await customerInfoAdd(this.createForm);
        if (response.data.code === 200) {
          ElMessage.success('新增客户成功');
          this.closeCreateModal();
          this.load(); // 重新加载列表
        } else {
          ElMessage.error(response.data.msg || '新增失败');
        }
      } catch (error) {
        console.error('新增客户失败:', error);
        ElMessage.error('新增客户失败，请稍后重试');
      }
    },
    async viewDetail(item) {
      try {
        const response = await customerInfoDetail(item.id);
        if (response.data.code === 200) {
          this.detailData = response.data.data;
          this.showDetailModal = true;
        } else {
          ElMessage.error(response.data.msg || '获取详情失败');
        }
      } catch (error) {
        console.error('获取客户详情失败:', error);
        ElMessage.error('获取客户详情失败，请稍后重试');
      }
    },
         closeDetailModal() {
       this.showDetailModal = false;
       this.detailData = {};
     },
     async toggleStatus(item) {
       try {
         const newStatus = item.status === 0 ? 1 : 0;
         const response = await customerInfoUpdateStatus(item.id, newStatus);
         if (response.data.code === 200) {
           item.status = newStatus;
           ElMessage.success(`状态已${newStatus === 0 ? '启用' : '禁用'}`);
         } else {
           ElMessage.error(response.data.msg || '状态更新失败');
         }
       } catch (error) {
         console.error('状态更新失败:', error);
         ElMessage.error('状态更新失败，请稍后重试');
       }
     },
     async loadUserList() {
       try {
         const response = await getUserList();
         if (response.data.code === 200) {
           this.userList = response.data.data || [];
         }
       } catch (error) {
         console.error('加载用户列表失败:', error);
       }
     }
  }
}
</script>

<style scoped>
.customers-to-be-reconciled-container {
  padding: 20px;
  background-color: #f5f7fa;
}

.controls-section {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 20px;
  align-items: center;
}

.btn-new {
    background-color: #409eff;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
}

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: center;
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-item label {
  font-size: 14px;
  white-space: nowrap;
}

.filter-item input, .filter-item select {
  padding: 8px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  width: 150px;
}

.actions {
  margin-left: auto;
}

.btn-primary {
  background-color: #409eff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.btn-secondary {
  background-color: #fff;
  color: #606266;
  border: 1px solid #dcdfe6;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 10px;
}

.table-section {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #ebeef5;
}

th {
  background-color: #f8f9fa;
}

.link-style {
    color: #409eff;
    cursor: pointer;
}

.link-style.disabled-text {
    color: #c0c4cc;
    cursor: pointer;
}

.status-toggle {
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.status-enabled {
    background-color: #f0f9ff;
    color: #409eff;
    border: 1px solid #409eff;
}

.status-enabled:hover {
    background-color: #409eff;
    color: white;
}

.status-disabled {
    background-color: #fef2f2;
    color: #f56c6c;
    border: 1px solid #f56c6c;
}

.status-disabled:hover {
    background-color: #f56c6c;
    color: white;
}

.footer-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  background-color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
}

.footer-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pagination button {
  padding: 6px 12px;
  border: 1px solid #dcdfe6;
  background-color: #fff;
  cursor: pointer;
  border-radius: 4px;
}

.pagination button.active {
  background-color: #409eff;
  color: white;
  border-color: #409eff;
}

.pagination select {
  padding: 6px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-link {
  background: none;
  border: none;
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}

.btn-link:hover {
  color: #66b1ff;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  border-radius: 12px;
  width: 90%;
  max-width: 700px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 24px 20px;
  border-bottom: 1px solid #ebeef5;
  background: lightskyblue;
  border-radius: 12px 12px 0 0;
}

.modal-header h3 {
  margin: 0;
  color: white;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.8);
  transition: all 0.3s ease;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.close-btn:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

.modal-body {
  padding: 24px;
  background-color: #fafbfc;
}

.form-row {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  align-items: flex-start;
}

.form-group {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-group label {
  margin-bottom: 8px;
  font-weight: 600;
  color: #303133;
  font-size: 14px;
}

.form-group label.required::after {
  content: ' *';
  color: #f56c6c;
}

.form-group input,
.form-group textarea,
.form-group select {
  padding: 10px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.form-group select {
  background-color: #fff;
  cursor: pointer;
}

.form-group textarea {
  resize: vertical;
  min-height: 80px;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
  transform: translateY(-1px);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.form-actions button {
  padding: 10px 24px;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.3s ease;
  min-width: 80px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.detail-item label {
  font-weight: 500;
  color: #606266;
  font-size: 14px;
}

.detail-item span {
  color: #303133;
  font-size: 14px;
  padding: 8px 0;
}

/* 详情弹窗样式 */
/* 右侧抽屉样式 */
.drawer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.35);
  display: flex;
  justify-content: flex-end;
  align-items: stretch;
  z-index: 1000;
}

.drawer-content {
  background-color: #fff;
  width: 520px;
  max-width: 85vw;
  height: 100%;
  box-shadow: -2px 0 12px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  animation: slideInFromRight 0.25s ease-out;
}

@keyframes slideInFromRight {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #ebeef5;
  background-color: #ffffff;

}

.detail-header h2 {
  margin: 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.detail-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.action-btn {
  background: none;
  border: none;
  padding: 8px 12px;
  cursor: pointer;
  color: #606266;
  font-size: 14px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.action-btn:hover {
  background-color: #f5f7fa;
}

.detail-body {
  padding: 24px;
}

.alert-box {
  background-color: #fff7e6;
  border: 1px solid #ffd591;
  border-radius: 6px;
  padding: 12px 16px;
  margin-bottom: 24px;
  color: #d46b08;
  font-size: 14px;
}

.detail-section {
  margin-bottom: 32px;
}

.detail-section h3 {
  margin: 0 0 16px 0;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 8px;
  border-bottom: 2px solid #409eff;
}

.detail-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-field label {
  font-weight: 500;
  color: #606266;
  font-size: 14px;
}

.form-field label.required::after {
  content: ' *';
  color: #f56c6c;
}

.form-field input {
  padding: 10px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  background-color: #f5f7fa;
  color: #606266;
}

.form-field input:read-only {
  background-color: #f5f7fa;
  cursor: not-allowed;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .form-row {
    flex-direction: column;
    gap: 16px;
  }

  .modal-content {
    width: 95%;
    margin: 10px;
  }

  .modal-body {
    padding: 16px;
  }

  .detail-grid {
    grid-template-columns: 1fr;
  }

  .detail-form {
    grid-template-columns: 1fr;
  }

  .detail-header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .detail-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .detail-modal-content {
    width: 98%;
    margin: 10px;
  }
}
</style>
